﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;

namespace SZ
{
    public partial class ImportRaster : Form
    {
        public ImportRaster()
        {
            InitializeComponent();

            DevExpress.XtraGrid.Columns.GridColumn colPath = new DevExpress.XtraGrid.Columns.GridColumn();
            colPath.Name = "colPath";
            colPath.Caption = "图层路径";
            colPath.FieldName = "colPath";
            colPath.VisibleIndex = 1; //这个是必须的，否则会无法显示
            colPath.Width = 200;

            DevExpress.XtraGrid.Columns.GridColumn colType = new DevExpress.XtraGrid.Columns.GridColumn();
            colType.Name = "colType";
            colType.Caption = "图层类型";
            colType.FieldName = "colType";
            colType.VisibleIndex = 2;
            colType.Width = 50;

            this.gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colPath, colType });
            this.gridView1.BestFitColumns();
        }

        private void addRasterLayerBtn_Click(object sender, EventArgs e)
        {
            AddDataDialogFrm addRasterFrm = new AddDataDialogFrm();
            addRasterFrm.ShowDialog();
            //rasterDataTable是static类型的，第一次需要创建列，以后就不需要
            if (PublicVariablesClass.rasterDataTable.Columns.Count == 0)
            {
                PublicVariablesClass.rasterDataTable.Columns.Add("colPath", typeof(string));
                PublicVariablesClass.rasterDataTable.Columns.Add("colType", typeof(string));
            }
            //判断选择栅格数据的数量
            for (int i = 0; i < 10; i++)
            {
                if (PublicVariablesClass.AddRasterFilePath[i] == null)
                {
                    this.gridView1.GridControl.DataSource = PublicVariablesClass.rasterDataTable;
                    return;
                }
                DataRow dataRow = PublicVariablesClass.rasterDataTable.Rows.Add();
                string rasterType;
                int index = PublicVariablesClass.AddRasterFilePath[i].LastIndexOf(".");
                rasterType = PublicVariablesClass.AddRasterFilePath[i].Substring(index + 1);
                if (rasterType.ToUpper() == "JPG" || rasterType.ToUpper() == "BMP" || rasterType.ToUpper() == "TIF" || rasterType.ToUpper() == "GIF")
                {
                    dataRow["colPath"] = PublicVariablesClass.AddRasterFilePath[i];
                    dataRow["colType"] = rasterType;
                }
                else
                {
                    MessageBox.Show("请选择栅格数据！");
                }
            }

        }

        private void deleteRasterLayerBtn_Click(object sender, EventArgs e)
        {
            int count = 0;//定义选中的栅格数据数量
            count = this.gridView1.SelectedRowsCount;
            int[] selectRows = this.gridView1.GetSelectedRows();
            DataRow dr;
            for (int i = 0; i < count; i++)
            {
                dr = this.gridView1.GetDataRow(selectRows[i]);
                dr.Delete();
            }

        }

        private void importRasterBtn_Click(object sender, EventArgs e)
        {
            //连接数据库
            ConnectSDE connectSde = new ConnectSDE();
            IWorkspace pWorkspace = connectSde.SdeWorkspace();

            int count = this.gridView1.RowCount;
            DataRow dataRow;
            for (int i = 0; i < count; i++)
            {
                dataRow = this.gridView1.GetDataRow(i);
                string LayerPath = dataRow["colPath"].ToString();
                string LayerName = LayerPath.Substring(LayerPath.LastIndexOf("\\") + 1);

                bool result = false;

                result = ImportToRasterCatalog(targetDatasetCombo.Text, LayerPath, pWorkspace);

                if (result)
                {
                    MessageBox.Show("入库成功！");
                }
                else
                {
                    if (targetDatasetCombo.Text == "")
                    {
                        MessageBox.Show("入库不成功，请选择一个目标要素集！");
                    }
                    else
                    {
                        MessageBox.Show("入库不成功！");
                    }
                }
            }

            progressBar1.Maximum = 100;//设置最大长度值
            progressBar1.Value = 0;//设置当前值
            progressBar1.Step = 5;//设置没次增长多少
            for (int i = 0; i < 10; i++)//循环
            {
                System.Threading.Thread.Sleep(1000);//暂停1秒
                progressBar1.Value += progressBar1.Step;//让进度条增加一次

            }
        }

        //栅格入库
        private bool ImportToRasterCatalog(string CatalogName, string RasterFilePath, IWorkspace pWorkspace)
        {
            ITrackCancel pTrackCancel = new TrackCancelClass();
            IRasterCatalogLoader pRasCatalogLoader = new RasterCatalogLoader();
            pRasCatalogLoader.ConnectionProperties = pWorkspace.ConnectionProperties;
            try
            {
                pRasCatalogLoader.LoadDatasets(CatalogName, RasterFilePath, pTrackCancel);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void ImportRaster_Load(object sender, EventArgs e)
        {
            //连接数据库
            ConnectSDE conncetSde = new ConnectSDE();
            IWorkspace pWorkspace = conncetSde.SdeWorkspace();

            //查找pWorkspace中的栅格目录
            IEnumDataset pRaseterEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTRasterCatalog);
            IDataset pRasterDataset = pRaseterEnumDataset.Next();
            while (pRasterDataset != null)
            {
                string rasterCatalogName = pRasterDataset.Name;
                targetDatasetCombo.Items.Add(rasterCatalogName);
                pRasterDataset = pRaseterEnumDataset.Next();
            }
        }




    }
}
